package com.test.personalblogproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.personalblogproject.mapper.ArticleMapper;
import com.test.personalblogproject.mapper.FavoritesItemMapper;
import com.test.personalblogproject.mapper.UserMapper;
import com.test.personalblogproject.pojo.*;
import com.test.personalblogproject.service.FavoritesItemService;
import com.test.personalblogproject.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class FavoritesItemServiceImpl extends ServiceImpl<FavoritesItemMapper,FavoritesItem>implements FavoritesItemService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FavoritesItemMapper favoritesItemMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public Result saveArticleToList(Long articleId, Long collectionId) {

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User sysUser = userMapper.findByUserName(username);
        FavoritesItem favoritesItem=new FavoritesItem();
        favoritesItem.setCollectionId(collectionId);
        favoritesItem.setUserId(String.valueOf(sysUser.getId()));
        favoritesItem.setArticleId(articleId);
        save(favoritesItem);
        return Result.success("成功收藏文章");


    }

    @Override
    public Result unbookmarkTtheArticle(Long articleId) {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User sysUser = userMapper.findByUserName(username);
        String userId=String.valueOf(sysUser.getId());
        LambdaQueryWrapper<FavoritesItem> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FavoritesItem::getArticleId,articleId)
                        .eq(FavoritesItem::getUserId,userId);
        remove(lambdaQueryWrapper);
        return Result.success("成功取消文章");

    }

    @Override
    public Result CheckIfTheArticleIsSaved(Long articleId) {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User sysUser = userMapper.findByUserName(username);
        String userId=String.valueOf(sysUser.getId());
        int count=favoritesItemMapper.isArticleCollected(userId,articleId);
        if(count==0)return Result.success("false(没有收藏）");
        else return Result.success("true(已经收藏）");


    }

    @Override
    public Result viewArticlesUnderTheCollectionList(Long collectionId) {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User sysUser = userMapper.findByUserName(username);
        String userId=String.valueOf(sysUser.getId());
        List<Long> articleIds=favoritesItemMapper.viewArticlesUnderTheCollectionList(userId,collectionId);
        if(articleIds==null) return Result.success("暂时没有收藏文章");

        return Result.success(articleMapper.selectBatchIds(articleIds));




    }
}
